package com.ultrapower.request;

import java.util.List;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;

import com.ultrapower.bean.ApplicationBean;
import com.ultrapower.mapper.ApplicationNameMapper;
import com.ultrapower.util.HBaseTables;

/**
 * 
 * @author wangy
 *
 */
public class ApplicationIndexTable extends BaseRequest {

	public static void main(String[] args) {
		BaseRequest applicationIndexTable = new ApplicationIndexTable();
		applicationIndexTable.execute();
//		ApplicationIndexTable applicationIndexTable = new ApplicationIndexTable();
//		applicationIndexTable.getExecute();
	}

	@Override
	void execute() {
		TableName applicationIndexTableName = this.getTableName(HBaseTables.APPLICATION_INDEX_STR);
		this.getHbaseTemplate().find(applicationIndexTableName, createScan(), (ResultScanner scanner) -> {

			ApplicationNameMapper map = new ApplicationNameMapper();
			scanner.forEach(r -> {
				try {
					List<ApplicationBean> info = map.mapRow(r, 0);
					if (info != null) {
						info.forEach(action -> {
							System.out.println(info);
						});
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
			return null;
		});

	}

	void getExecute() {
		TableName applicationIndexTableName = this.getTableName(HBaseTables.APPLICATION_INDEX_STR);
		this.getHbaseTemplate().get(applicationIndexTableName, createGet(), (r) -> {
			ApplicationNameMapper map = new ApplicationNameMapper();
			try {
				List<ApplicationBean> info = map.mapRow(r, 0);
				if (info != null) {
					info.forEach(action -> {
						System.out.println(info);
					});
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		});

	}

	@Override
	protected Scan createScan() {
		Scan scan = new Scan();
		scan.setCaching(30);
		return scan;
	}

	protected Get createGet() {
		Get get = new Get(this.applicationName.getBytes());
		return get;
	}

}
